package com.zl.autocode.dao.generator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.common.utils.generator.model.ConvertColumn;
import com.zl.autocode.common.utils.generator.model.ConvertTable;

import cn.hutool.extra.spring.SpringUtil;

@Service("sqlite"+AutocodeConfigC.GENERATOR_DAO_KEY)
public class SqliteGeneratorDao implements IGeneratorDao{
private static Logger LOGGER = LoggerFactory.getLogger(SqliteGeneratorDao.class);
    private Connection con =null;
    private Statement st =null;
    @Override
    public List<ConvertTable> queryTables(String tableCodes, String type,boolean endFlag) throws Exception {
        List<ConvertTable> list=new ArrayList<>();
        try{
            String tbls= "";
            if(StringUtils.isNotBlank(tableCodes)){
                tbls= " AND tbl_name like '"+tableCodes+"'";
            }
            if(con==null){
                con = SpringUtil.getBean(DataSource.class).getConnection();
                st = con.createStatement();
            }
            ResultSet rs = st.executeQuery("SELECT name FROM sqlite_master WHERE type = 'table' "+tbls);
            while (rs.next()){
                ConvertTable vo=new ConvertTable();
                vo.setTableCode(rs.getString("name"));
                list.add(vo);
            }
            rs.close();
        }catch(Exception e){
            LOGGER.error("数据库查询异常{}",e);
            if(con!=null){
                st.close();
                con.close();
            }
        }finally{
            if(endFlag&&con!=null){
                st.close();
                con.close();
            }
        }
        return list;
    }

    @Override
    public List<ConvertColumn> queryTableColumns(String tableCode,boolean endFlag) throws Exception {
        List<ConvertColumn> list=new ArrayList<>();
        try{
            if(con==null){
                con = SpringUtil.getBean(DataSource.class).getConnection();
                st = con.createStatement();
            }
            ResultSet rs = st.executeQuery("PRAGMA table_info("+tableCode+")");
            while (rs.next()){
                ConvertColumn vo=new ConvertColumn();
                vo.setColumnCode(rs.getString("name"));
                vo.setColumnDatatype(rs.getString("type"));
                vo.setColumnPrimarykey(rs.getString("pk"));
                vo.setColumnMandatory("1".equals(rs.getString("notnull"))?"0":"1");
                vo.setColumnDefault(rs.getString("dflt_value"));
                list.add(vo);
            }
            rs.close();
        }catch(Exception e){
            LOGGER.error("数据库查询异常{}",e);
            if(con!=null){
                st.close();
                con.close();
            }
        }finally{
            if(endFlag&&con!=null){
                st.close();
                con.close();
            }
        }
        return list;
    }
}